package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.bean.LoginBean
import com.xgst.driverlesscars.api.ApiResponse
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class ReceiveBuildViewModel : BaseViewModel() {
    fun getUserName(name:String,psw:String?,onSuccess:(String?)->Unit){
        viewModelScope.launch {
            var bean: ApiResult<ApiResponse<LoginBean>>?
            if(psw == null){
                bean = mApi.getUserNameForUID(name)
            }else{
                bean = mApi.getUserNameForAccount(name,psw)
            }
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data?.name)
                is ApiResult.Error -> ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    fun submit(map:MutableMap<String,String>,onSuccess:(String)->Unit,onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.submitYangBen(map)
            when(bean){
                is ApiResult.Success -> onSuccess("")
                is ApiResult.Error ->onError(bean.errorMessage)
            }
        }
    }
}
